package com.hss01248.dialog;

import ohos.aafwk.ability.Ability;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * Created by Administrator on 2017/2/15 0015.
 *
 * 参考:https://zhuanlan.zhihu.com/p/25221428
 */

public final class ActivityStackManager {


    private static ActivityStackManager sInstance = new ActivityStackManager();
    private WeakReference<Ability> topAttachedActivityWeakRef;
    private static Stack<Ability> mActivityStack = new Stack<>();
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "dialog");

    private ActivityStackManager() {

    }

    public static ActivityStackManager getInstance() {
        return sInstance;
    }


    /**
     * activity from oncreate callback is useable for dialog ,but not usable for popupwindow
     * @return
     */
    public Ability getTopActivity() {
        Ability currentActivity = null;
        if(mActivityStack.size()>0){
            currentActivity = mActivityStack.get(mActivityStack.size()-1);
        }
        return currentActivity;
    }

    public Ability getTopActivity(Class activityExpected){
        Ability activity = getTopActivity();
        if(activity !=null && activity.getClass().equals(activityExpected)){
            return activity;
        }else {
            return null;
        }
    }

    public Ability getActivity(Class activityExpected){
        if (mActivityStack == null) {
            return null;
        }
        for (Ability activity1 : mActivityStack) {
            if (activity1.getClass().equals(activityExpected)) {
                return activity1;
            }
        }
        return null;
    }

    /**
     *
     * @param idxTop 从上往下的index,从1开始
     * @return
     */
    public boolean finishActivityByIdx(int idxTop){
        if(mActivityStack.isEmpty()){
            return false;
        }
        int index = mActivityStack.size() -idxTop;
        Ability activity =  mActivityStack.remove(index);
       if(activity !=null && !activity.isTerminating()){
           activity.terminateAbility();
           return true;
       }
       return true;
    }


    /**
     * 在基类activity 的 onAttachToWindow时调用
     * @param activity
     */
    public void setTopAttached(Ability activity) {
        topAttachedActivityWeakRef = new WeakReference<Ability>(activity);
    }

    /**
     * 在基类activity 的 onDetachToWindow时调用
     * @param activity
     */
    public void removeTopAttached(Ability activity){
        if(activity ==null){
            return;
        }
        if(topAttachedActivityWeakRef!=null){
            Ability activity1 =  topAttachedActivityWeakRef.get();
            if(activity.equals(activity1)){
                topAttachedActivityWeakRef = null;
            }

        }
    }

    /**
     * if return in not null,then it can be used for a popupwindow
     * @return
     */
    public Ability getTopAttached(){
        if(topAttachedActivityWeakRef!=null){
            Ability activity =  topAttachedActivityWeakRef.get();
            if(isUsable(activity)){
                return activity;
            }
        }
        return null;
    }

    public static boolean isUsable(Ability activity) {
        if(activity ==null){
            return false;
        }

        if(activity.isTerminating()){
            return false;
        }

        return true;
    }


    /**
     * 返回栈大小
     *
     * @return 大小
     */
    public int size() {
        return mActivityStack.size();
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Ability activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<>();
        }
        if (activity != null) {
            mActivityStack.add(activity);
            HiLog.error(LABEL, "mActivityStack.size()--addActivity:"+activity.getClass().getSimpleName()+mActivityStack.size());
        }
    }

    /**
     * 移除activity
     */
    public void removeActivity(Ability activity) {
        mActivityStack.remove(activity);
        DialogsMaintainer.onDestory(activity);
        HiLog.error(LABEL, "mActivityStack.size()--removeActivity:"+activity.getClass().getSimpleName()+mActivityStack.size());
    }

    /**
     * 判断某个activity是否还存活
     *
     * @param cls
     * @return
     */
    public boolean isActivityAlive(Class<?> cls) {
        if (mActivityStack == null) {
            return false;
        }

        for (Ability activity1 : mActivityStack) {
            if (activity1.getClass().equals(cls)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Ability activity) {
        if (mActivityStack == null) {
            return;
        }

        if (activity != null) {
            mActivityStack.remove(activity);
            activity.terminateAbility();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (mActivityStack == null) {
            return;
        }

        for (Ability activity : mActivityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }
}
